<?php
/**
 *  Abstract reference property for referenced single objects
 *  Examples of Reference: ForeignKey, OneToOne
 *
 */
abstract class Yz_Model_Field_Reference
    extends Yz_Model_Field_Abstract
{

    protected $model;
    protected $itemClass;

    /**
     *  @param Yz_Model_Interface|null $model => null for 'compatible' __construct
     */
    public function __construct( $config=null )
    {
        debug();
        $args = func_get_args();
        $model = array_shift($args);
        if (! ($model instanceof Yz_Model_Interface))
            throw TypeError::_new_();
        $this->model = $model;
        $this->itemClass = $model->getItemClass();
        assert('is_string($this->itemClass) and $this->itemClass !== ""');
        parent::__construct($args);
    }

    /**
     *
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     *
     */
    public function getItemClass()
    {
        return $this->itemClass;
    }


    /** Initialization value: can not initialize reference fields
     *  @return
     */
    public function assignValue($value)
    {
        throw StateError::_new_();
    }


    /** Test if a value is suitable for the property, possibly adjust
     *  @param mixed value
     *  @return the adjusted value
     *  @throw TypeError
     *  @throw NullValueError
     */
    protected function _testValue($value)
    {
        if (! ($value instanceof $this->itemClass))
            throw TypeError::_new_();
        return $value;
    }


}
